/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generate.service.impl.GenerateOrderServiceImpl
   Module Description   :

   Date Created      : 2010/1/4
   Original Author   : feeling.wang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generate.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.service.GenerateOrderService;
import com.yaodian100.ec.order.entity.OrderPrize;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.entity.ServiceOrders;
import com.yaodian100.ec.order.service.OrderPrizeService;
import com.yaodian100.ec.order.service.OrderService;
import com.yaodian100.ec.order.service.ServiceOrderService;
import com.yaodian100.ec.shelf.service.ShelfTuangouService;

/**
 * @author feeling.wang
 * 
 */
public class GenerateOrderServiceImpl implements GenerateOrderService {
	private Menu orderStMenu;
	private Menu paymentStMenu;
	private Menu paymentTpMenu;
	private Menu logisticStMenu;
	private Menu returnStMenu;

	private DomainService<Menu> menuService; // 與 setter
	private OrderService orderService;
	private TemplateService velocityService;
	private List<Orders> orders;
	private List<OrderPrize> orderPrizes;
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Resource
	private ServiceOrderService serviceOrderService;
	@Resource
	private OrderPrizeService orderPrizeService;
	@Resource
	private ShelfTuangouService shelfTuangouService;

	private static String zeroToFour;
	private static String fiveToEight;

	private static final String subFileName = ".html";
	/** pageRecord, 每頁筆数 */
	private final int pageRecord = 10;
	/** totalSize, 全部筆數 */

	private final int unitPage = 10; // 每十頁為一個單位

	public Menu getOrderStMenu() {
		return orderStMenu;
	}

	public void setOrderStMenu(Menu orderStMenu) {
		this.orderStMenu = orderStMenu;
	}

	public Menu getPaymentStMenu() {
		return paymentStMenu;
	}

	public void setPaymentStMenu(Menu paymentStMenu) {
		this.paymentStMenu = paymentStMenu;
	}

	public Menu getPaymentTpMenu() {
		return paymentTpMenu;
	}

	public void setPaymentTpMenu(Menu paymentTpMenu) {
		this.paymentTpMenu = paymentTpMenu;
	}

	public Menu getLogisticStMenu() {
		return logisticStMenu;
	}

	public void setLogisticStMenu(Menu logisticStMenu) {
		this.logisticStMenu = logisticStMenu;
	}

	public Menu getReturnStMenu() {
		return returnStMenu;
	}

	public void setReturnStMenu(Menu returnStMenu) {
		this.returnStMenu = returnStMenu;
	}

	@Override
	public void generateMemberOrderPage(String memberId) throws CoreException {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.service.GenerateOrderService#generateMembersOrderPage()
	 */
	@Override
	public void generateMembersOrderPage() throws CoreException {

	}

	@Override
	public Map<String, Object> getHistoryOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			int totalSize = orderService.getListSize(criteriaH).intValue();
			// if (totalSize > 0) {
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "H", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "H");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			// for (int i = 0; i < pager.getPageSize(); i++) {
			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaH,
					new String[] { "orderDt" });
			orders = findSplitOrder(orders); // 查询已拆分之分批出货订单
			infoMap.put("orders", orders);
			// infoMap.put("orderListString", setOrderListString(orders));

			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_H", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}
			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	/**
	 * 找出被拆分的订单
	 * 
	 * @param orders
	 * @return
	 */
	private List<Orders> findSplitOrder(List<Orders> orders) {
		for (int i = 0; i < orders.size(); i++) {
			Orders tmporder = orders.get(i);
			if ("83".equals(tmporder.getOrderSt())) {
				CommonCriteria criteria = new CommonCriteria();
				criteria.addEq("orgOrderId", tmporder.getOrderId());
				List<Orders> splitOrders = orderService.getList(0, -1, criteria, new String[] { "orderDt" });
				if (null != splitOrders && !splitOrders.isEmpty()) {
					orders.addAll(i + 1, splitOrders);
				}
				i += splitOrders.size();
			}
		}
		return orders;
	}

	@Override
	public Map<String, Object> getActiveOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			int totalSize = getOrderCount(criteriaT);
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "T", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "T");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaT,
					new String[] { "orderDt" });
			orders = findSplitOrder(orders);// 查询已拆分之分批出货订单
			infoMap.put("orders", orders);

			// infoMap.put("orderListString", setOrderListString(orders));
			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_T", totalSize);
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}

			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			e.printStackTrace();
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	@Override
	public Map<String, Object> getYetPaymentOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaY = getOrderCommonCriteria(memberId, "Y");
			int totalSize = orderService.getListSize(criteriaY).intValue();

			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "Y", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "Y");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaY,
					new String[] { "orderDt" });
			infoMap.put("orders", orders);

			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_Y", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}

			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	@Override
	public Map<String, Object> getTuangouOrders(String memberId, int page, String pageType) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			// GH,GY,GT
			String queryType = StringUtils.right(pageType, 1);
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, queryType);
			criteriaG.addNe("tuangouId", null);
			int totalSize = getOrderCount(criteriaG);
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "T", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", pageType);
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaG,
					new String[] { "orderDt" });
			infoMap.put("orders", orders);

			infoMap.put("currentPage", pager.getCurrentPage() + 1);

			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}

			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			// criteriaG.addNe("tuangouId", null);// 團購判別
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			// criteriaG.addNe("tuangouId", null);// 團購判別
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 團購交易中订单
			CommonCriteria criteriaGT = getOrderCommonCriteria(memberId, "T");
			criteriaGT.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaGT);
			infoMap.put("totalCount_G", tuangouOrderCount);// 用於顯示tab標頭訂單數
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			e.printStackTrace();
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	/**
	 * 依類別組出查詢條件
	 * 
	 * @param memberId
	 * @param orderType
	 * @return
	 * @throws CoreException
	 * @throws ParseException
	 */
	private CommonCriteria getOrderCommonCriteria(String memberId, String orderType) throws CoreException, ParseException {
		CommonCriteria commonCriteria = new CommonCriteria();
		commonCriteria.addEq("memberId", memberId);
		if ("T".equalsIgnoreCase(orderType)) {
			commonCriteria.addGe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MIN_PATTERN_TIMESTAMP)
					.format(DateUtils.addDays(new Date(), -90)), new String[] { EcConstant.PATTERN_TIMESTAMP }));
			commonCriteria.addLe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MAX_PATTERN_TIMESTAMP)
					.format(new Date()), new String[] { EcConstant.PATTERN_TIMESTAMP }));
		}
		if ("Y".equalsIgnoreCase(orderType)) {// 未付款訂單
			commonCriteria.addEq("paymentSt", "00");
			commonCriteria.addNe("paymentTp", "30");
			commonCriteria.addNe("paymentTp", "60");
			commonCriteria.addEq("orderSt", "10");
			commonCriteria.addGe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MIN_PATTERN_TIMESTAMP)
					.format(DateUtils.addDays(new Date(), -90)), new String[] { EcConstant.PATTERN_TIMESTAMP }));
			commonCriteria.addLe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MAX_PATTERN_TIMESTAMP)
					.format(new Date()), new String[] { EcConstant.PATTERN_TIMESTAMP }));
		}
		if ("H".equalsIgnoreCase(orderType)) {// 歷史訂單
			commonCriteria.addLe("orderDt", DateUtils.addDays(new Date(), 0));
		}
		if ("HY".equalsIgnoreCase(orderType)) {// 未付款訂單
			commonCriteria.addEq("paymentSt", "00");
			commonCriteria.addNe("paymentTp", "30");
			commonCriteria.addNe("paymentTp", "60");
			commonCriteria.addEq("orderSt", "10");
			commonCriteria.addLe("orderDt", DateUtils.addDays(new Date(), 0));
		}
		if ("S".equalsIgnoreCase(orderType)) {// 拆分訂單
			commonCriteria.addEq("orderSt", "83");
			commonCriteria.addGe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MIN_PATTERN_TIMESTAMP)
					.format(DateUtils.addDays(new Date(), -90)), new String[] { EcConstant.PATTERN_TIMESTAMP }));
			commonCriteria.addLe("orderDt", DateUtils.parseDate(FastDateFormat.getInstance(EcConstant.MAX_PATTERN_TIMESTAMP)
					.format(new Date()), new String[] { EcConstant.PATTERN_TIMESTAMP }));
		}
		if ("HS".equalsIgnoreCase(orderType)) {// 拆分訂單
			commonCriteria.addEq("orderSt", "83");
			commonCriteria.addLe("orderDt", DateUtils.addDays(new Date(), 0));
		}
		return commonCriteria;
	}

	/**
	 * 查詢訂單筆數
	 * 
	 * @param criteria
	 * @return
	 * @throws CoreException
	 */
	private int getOrderCount(CommonCriteria criteria) throws CoreException {
		int count = orderService.getListSize(criteria).intValue();
		return count;
	}

	/**
	 * 查詢抽獎活動筆數
	 * 
	 * @param criteria
	 * @return
	 * @throws CoreException
	 */
	private int getOrderPrizeCount(CommonCriteria criteria) throws CoreException {
		int count = orderPrizeService.getListSize(criteria).intValue();
		return count;
	}

	@Override
	public int getOrderTypeCount(String memberId, String type) {
		CommonCriteria cc = new CommonCriteria();
		try {
			cc = getOrderCommonCriteria(memberId, type);
			return getOrderCount(cc);
		} catch (Exception e) {

			e.printStackTrace();
			return -1;
		}

	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setVelocityService(TemplateService velocityService) {
		this.velocityService = velocityService;
	}

	@Override
	public Map<String, Object> getMobileOrders(String memberId) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			int totalSize = getOrderCount(criteriaT);
			// SimplePager pager = new SimplePager();
			// pager.setPageRecord(pageRecord);
			// pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "T", totalSize);

			// infoMap.put("totalPage", pager.getPageSize());
			// infoMap.put("orderType", "T");
			// infoMap.put("totalCount", totalSize);
			// infoMap.put("pageRecord", pageRecord);
			// infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			// pager.setCurrentPage(page);
			// orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaT,
			// new String[] { "orderDt" });
			orders = orderService.getList(0, -1, criteriaT, new String[] { "orderDt" });
			orders = findSplitOrder(orders);// 查询已拆分之分批出货订单
			infoMap.put("orders", orders);

			// infoMap.put("orderListString", setOrderListString(orders));
			// infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_T", totalSize);
			// calcPage(page, pager.getPageSize());
			// int k = page / unitPage;
			// int startPage = k * unitPage + 1;
			// int endPage = (k + 1) * unitPage;
			// if (endPage > pager.getPageSize()) {
			// endPage = pager.getPageSize();
			// }

			// infoMap.put("startPage", startPage);
			// infoMap.put("endPage", endPage);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			orders = orderService.getList(0, -1, criteriaH, new String[] { "orderDt" });
			orders = findSplitOrder(orders);// 查询已拆分之分批出货订单
			infoMap.put("ordersH", orders);
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			// CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			// criteriaG.addNe("tuangouId", null);// 團購判別
			// int tuangouOrderCount = getOrderCount(criteriaG);
			// infoMap.put("totalCount_G", tuangouOrderCount);

			// serviceOrder
			// CommonCriteria cc = new CommonCriteria();
			// cc.addEq("memberId", memberId);
			// List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			// infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			e.printStackTrace();
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	@Override
	public Map<String, Object> getTuangouOrderPrizes(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {

			CommonCriteria commonCriteria = new CommonCriteria();
			commonCriteria.addEq("memberId", memberId);
			int totalSize = orderPrizeService.getListSize(commonCriteria).intValue();
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "OP");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());
			pager.setCurrentPage(page);
			orderPrizes = orderPrizeService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(),
					commonCriteria, new String[] { "orderDate" });
			infoMap.put("orderPrizes", orderPrizes);
			// Map<String, String> tuangouStMap = new HashMap<String, String>();
			// for (OrderPrize orpz : orderPrizes) {
			// ShelfTuangou shelfTuangou = shelfTuangouService.getActiveShelfTuangouByTuangouId(orpz.getTuangouId());
			// if (null == shelfTuangou) {
			// tuangouStMap.put(orpz.getPrizeId().toString(), "01");
			// } else {
			// tuangouStMap.put(orpz.getPrizeId().toString(), "00");
			// }
			// }
			// infoMap.put("tuangouStMap", tuangouStMap);
			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_OP", pager.getTotalSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}
			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			// criteriaG.addNe("tuangouId", null);// 團購判別
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			// criteriaG.addNe("tuangouId", null);// 團購判別
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 團購交易中订单
			CommonCriteria criteriaGT = getOrderCommonCriteria(memberId, "T");
			criteriaGT.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaGT);
			infoMap.put("totalCount_G", tuangouOrderCount);// 用於顯示tab標頭訂單數

			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);
		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	public OrderPrizeService getOrderPrizeService() {
		return orderPrizeService;
	}

	public void setOrderPrizeService(OrderPrizeService orderPrizeService) {
		this.orderPrizeService = orderPrizeService;
	}

	public ShelfTuangouService getShelfTuangouService() {
		return shelfTuangouService;
	}

	public void setShelfTuangouService(ShelfTuangouService shelfTuangouService) {
		this.shelfTuangouService = shelfTuangouService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.service.GenerateOrderService#getSplitOrders(java.lang.String, int)
	 */
	@Override
	public Map<String, Object> getSplitOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaS = getOrderCommonCriteria(memberId, "S");
			// criteriaS.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			int totalSize = orderService.getListSize(criteriaS).intValue();
			// if (totalSize > 0) {
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "H", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "S");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			// for (int i = 0; i < pager.getPageSize(); i++) {
			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaS,
					new String[] { "orderDt" });
			orders = findSplitOrder(orders); // 查询已拆分之分批出货订单
			infoMap.put("orders", orders);
			// infoMap.put("orderListString", setOrderListString(orders));

			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_H", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}
			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.service.GenerateOrderService#getHistoryOrdersSelect(java.lang.String, int)
	 */
	@Override
	public Map<String, Object> getHistoryOrdersSelect(String memberId, int page, String parm) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();

		String oId = "[0-9]{13}";// 訂單號
		String pId = "[0-9]{8}";// 商品編號
		String pName = "^[0-9_]+$";
		boolean select = false;
		try {
			CommonCriteria criteriaHPS = new CommonCriteria();
			if (parm.matches(pName)) {
				if (parm.matches(oId)) {
					criteriaHPS.addEq("orderId", parm);
					criteriaHPS.addEq("memberId", memberId);
					select = true;
				} else if (parm.matches(pId)) {
					String sql = "select distinct a.order_id from ec_order_main a, ec_order_product b where a.order_id = b.order_id and b.prod_id = ? and a.member_id = ? ";
					List<Map<String, Object>> orderList = jdbcTemplate.queryForList(sql, parm, memberId);
					ArrayList<String> orderContainThisProd = new ArrayList<String>();
					if (!orderList.isEmpty()) {
						for (Iterator<Map<String, Object>> orders = orderList.iterator(); orders.hasNext();) {
							Map<String, Object> order = orders.next();
							String orderId = (String) order.get("order_id");
							orderContainThisProd.add(orderId);
						}
					}
					if (orderContainThisProd.size() > 0) {
						criteriaHPS.addIn("orderId", orderContainThisProd);
						criteriaHPS.addEq("memberId", memberId);
						select = true;
					} else {
						infoMap.put("errorMsg", "查无此商品编号的订单，请重新输入");
					}

				} else {
					infoMap.put("errorMsg", "您输入的订单编号或商品编号错误，请重新输入");
				}
			} else {
				String sql = "select distinct a.order_id from ec_order_main a, ec_order_product b where a.order_id = b.order_id and (b.prod_name like (?) or a.tuangou_name like (?)) and a.member_id = ? and a.split_type != '2' ";
				List<Map<String, Object>> orderList = jdbcTemplate.queryForList(sql, "%" + parm + "%", "%" + parm + "%",
						memberId);
				ArrayList<String> orderContainThisProd = new ArrayList<String>();
				if (!orderList.isEmpty()) {
					for (Iterator<Map<String, Object>> orders = orderList.iterator(); orders.hasNext();) {
						Map<String, Object> order = orders.next();
						String orderId = (String) order.get("order_id");
						orderContainThisProd.add(orderId);
					}
				}
				if (orderContainThisProd.size() > 0) {
					criteriaHPS.addIn("orderId", orderContainThisProd);
					criteriaHPS.addEq("memberId", memberId);
					select = true;
				} else {
					infoMap.put("errorMsg", "查无此商品名称的订单，请重新输入");
				}
			}
			SimplePager pager = new SimplePager();
			int totalSize = 0;
			if (select) {
				totalSize = orderService.getListSize(criteriaHPS).intValue();
				pager.setPageRecord(pageRecord);
				pager.setTotalSize(totalSize);
				pager.setCurrentPage(page);
				orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(),
						criteriaHPS, new String[] { "orderDt" });
				orders = findSplitOrder(orders); // 查询已拆分之分批出货订单
				infoMap.put("orders", orders);
				infoMap.put("totalCount", totalSize);
				infoMap.put("ordersize", orders.size());
				infoMap.put("selectparm", parm);
			} else {
				infoMap.put("orders", orders);
				infoMap.put("totalCount", 0);
			}

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "HSP");
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());
			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_H", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}
			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (Exception e) {
			throw new CoreException("seach產生錯誤");
		}
		return infoMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.service.GenerateOrderService#getHistorySplitOrders(java.lang.String, int)
	 */
	@Override
	public Map<String, Object> getHistorySplitOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaS = getOrderCommonCriteria(memberId, "HS");
			// criteriaS.addNe("splitType", "2"); // 分批出貨拆單預設不顯示
			int totalSize = orderService.getListSize(criteriaS).intValue();
			// if (totalSize > 0) {
			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "H", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "HS");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			// for (int i = 0; i < pager.getPageSize(); i++) {
			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaS,
					new String[] { "orderDt" });
			orders = findSplitOrder(orders); // 查询已拆分之分批出货订单
			infoMap.put("orders", orders);
			// infoMap.put("orderListString", setOrderListString(orders));

			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_H", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}
			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.generate.service.GenerateOrderService#getHistoryYetPaymentOrders(java.lang.String, int)
	 */
	@Override
	public Map<String, Object> getHistoryYetPaymentOrders(String memberId, int page) throws CoreException {
		Map<String, Object> infoMap = new HashMap<String, Object>();
		try {
			CommonCriteria criteriaY = getOrderCommonCriteria(memberId, "HY");
			int totalSize = orderService.getListSize(criteriaY).intValue();

			SimplePager pager = new SimplePager();
			pager.setPageRecord(pageRecord);
			pager.setTotalSize(totalSize);
			// infoMap = getCommonParamMap(pager.getPageSize(), "Y", totalSize);

			infoMap.put("totalPage", pager.getPageSize());
			infoMap.put("orderType", "HY");
			infoMap.put("totalCount", totalSize);
			infoMap.put("pageRecord", pageRecord);
			infoMap.put("frontContextPath", ServerValue.getFrontContextPath());

			pager.setCurrentPage(page);
			orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), criteriaY,
					new String[] { "orderDt" });
			infoMap.put("orders", orders);

			infoMap.put("currentPage", pager.getCurrentPage() + 1);
			infoMap.put("totalCount_Y", pager.getTotalSize());
			// calcPage(page, pager.getPageSize());
			int k = page / unitPage;
			int startPage = k * unitPage + 1;
			int endPage = (k + 1) * unitPage;
			if (endPage > pager.getPageSize()) {
				endPage = pager.getPageSize();
			}

			infoMap.put("startPage", startPage);
			infoMap.put("endPage", endPage);
			// 交易中订单
			CommonCriteria criteriaT = getOrderCommonCriteria(memberId, "T");
			criteriaT.addNe("splitType", "2");
			int orderCount = getOrderCount(criteriaT);
			infoMap.put("totalCount_T", orderCount);
			// 历史订单筆數
			CommonCriteria criteriaH = getOrderCommonCriteria(memberId, "H");
			criteriaH.addNe("splitType", "2");
			int historyPaymentOrderCount = getOrderCount(criteriaH);
			infoMap.put("totalCount_H", historyPaymentOrderCount);
			// 團購交易中订单
			CommonCriteria criteriaG = getOrderCommonCriteria(memberId, "T");
			criteriaG.addNe("tuangouId", null);// 團購判別
			int tuangouOrderCount = getOrderCount(criteriaG);
			infoMap.put("totalCount_G", tuangouOrderCount);
			// 抽獎活動
			CommonCriteria criteriaOP = new CommonCriteria();
			criteriaOP.addEq("memberId", memberId);
			int tuangouOrderPrizeCount = getOrderPrizeCount(criteriaOP);
			infoMap.put("totalCount_OP", tuangouOrderPrizeCount);
			// serviceOrder
			CommonCriteria cc = new CommonCriteria();
			cc.addEq("memberId", memberId);
			List<ServiceOrders> so = serviceOrderService.getList(0, -1, cc, new String[] { "orderId" });
			infoMap.put("serviceOrders", so);

		} catch (ParseException e) {
			throw new CoreException("日期轉換錯誤");
		} catch (Exception e) {
			throw new CoreException("Menu產生錯誤");
		}
		return infoMap;
	}
}
